﻿using AutoMapper;
using MediatR;
using System.Collections.Generic;
using Traceability.App.Api.Read.Application.Command.Trace;
using Traceability.App.Domain.新文件夹;
using Traceability.App.ErrorCode;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Api.Read.Application.Handler.Trace
{
    public class PiDelHandler : IRequestHandler<PiDelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<TraceabilityModel> traceRepo;
        private readonly IMapper mapper;

        public PiDelHandler(IBaseRepository<TraceabilityModel> traceRepo, IMapper mapper)
        {
            this.traceRepo = traceRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(PiDelCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var lists = request.ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            foreach (var item in lists)
            {
                var list = traceRepo.GetAll().FirstOrDefault(x => x.Id == item);
                list.IsDel = !list.IsDel;
                await traceRepo.Update(list);
            }
            res.Message = "批量删除成功";
            res.Code = ApiEnum.Ok;

            return res;
        }
    }
}
